﻿module Application
    open System.Windows.Forms
    open System.Drawing

    let f = new Form()
    f.TopMost <- true
    
    let pnl = new MathVisualizer.Controls.mathPanel()
    f.Controls.Add(pnl)
    
    f.Size <- new Size(720, 430)
    pnl.Anchor <- AnchorStyles.Top
    pnl.Dock <- DockStyle.Fill
    pnl.Size <- f.ClientSize
    
    pnl.setTitle "Documento di prova"
    let m = new MainMenu()
    let mfile = m.MenuItems.Add("File")
    let msave = new MenuItem("&Salva", new System.EventHandler(fun sender e -> 
                                                            let dialog = new System.Windows.Forms.SaveFileDialog()
                                                            dialog.AddExtension <- true
                                                            dialog.DefaultExt <- "png"
                                                            dialog.Title <- "Salva immagine"
                                                            dialog.Filter <- "PNG (*.png)| *.png"
                                                            match dialog.ShowDialog() with
                                                                | DialogResult.OK -> 
                                                                    pnl.getCurrentImage.Save(dialog.FileName)
                                                                | _ -> ()
                                                            ()
                                                        ))
    let msaveindex = mfile.MenuItems.Add(msave)
    
    let medit = m.MenuItems.Add("Modifica")
    
    let copyimageindex = medit.MenuItems.Add(new MenuItem("Copia immagine", new System.EventHandler(fun _ _ -> System.Windows.Forms.Clipboard.SetImage(pnl.getCurrentImage))))
    let copyexprindex = medit.MenuItems.Add(new MenuItem("Copia espressione", new System.EventHandler(fun _ _ -> System.Windows.Forms.Clipboard.SetText(pnl.getCurrentExprString))))
    let pasteindex = medit.MenuItems.Add(new MenuItem("Incolla espressione", new System.EventHandler(fun _ _ -> 
                                                                                        let s = System.Windows.Forms.Clipboard.GetText()
                                                                                        pnl.insertText(s + " ")
                                                                                        pnl.moveCursor(s.Length)
                                                                                    )))
    let mexpr = m.MenuItems.Add("Espressioni")
    let mletter = m.MenuItems.Add("Lettere")
 
    let menuexpr = [| 
                    new MenuItem("Sum", new System.EventHandler(fun _ _ -> pnl.insertText("sum( , )"); pnl.moveCursor 4 ));
                    new MenuItem("Sub", new System.EventHandler(fun _ _ -> pnl.insertText("sub( , )"); pnl.moveCursor 4 )); 
                    new MenuItem("Mul", new System.EventHandler(fun _ _ -> pnl.insertText("mul( , )"); pnl.moveCursor 4 )); 
                    new MenuItem("Ratio", new System.EventHandler(fun _ _ -> pnl.insertText("ratio( , )"); pnl.moveCursor 6 ));
                    new MenuItem("Exponent", new System.EventHandler(fun _ _ -> pnl.insertText("exponent( , )"); pnl.moveCursor 9 ));
                    new MenuItem("Subscript", new System.EventHandler(fun _ _ -> pnl.insertText("subscript( , )"); pnl.moveCursor 10 ));
                    new MenuItem("Sqrt", new System.EventHandler(fun _ _ -> pnl.insertText("sqrt( , )"); pnl.moveCursor 5 ));
                    new MenuItem("Limit", new System.EventHandler(fun _ _ -> pnl.insertText("limit( , )"); pnl.moveCursor 6 ));
                    new MenuItem("Log", new System.EventHandler(fun _ _ -> pnl.insertText("log_{ }{ }"); pnl.moveCursor 5 ));
                    new MenuItem("-");
                    new MenuItem("BigSum", new System.EventHandler(fun _ _ -> pnl.insertText("bigsum( , ,)"); pnl.moveCursor 7 ));
                    new MenuItem("Integral", new System.EventHandler(fun _ _ -> pnl.insertText("int( , , )"); pnl.moveCursor 4 ));
                    new MenuItem("Productor", new System.EventHandler(fun _ _ -> pnl.insertText("productor( , , )"); pnl.moveCursor 10 ));
                    new MenuItem("Union", new System.EventHandler(fun _ _ -> pnl.insertText("union( , , )"); pnl.moveCursor 6 ));
                    new MenuItem("Intersection", new System.EventHandler(fun _ _ -> pnl.insertText("intersection( , , )"); pnl.moveCursor 13 ));
                    new MenuItem("-");
                    new MenuItem("Round bracket", new System.EventHandler(fun _ _ -> pnl.insertText("round( , )"); pnl.moveCursor 6 ));
                    new MenuItem("Square bracket", new System.EventHandler(fun _ _ -> pnl.insertText("square( , )"); pnl.moveCursor 7 ));
                    new MenuItem("Curly bracket", new System.EventHandler(fun _ _ -> pnl.insertText("brace( , )"); pnl.moveCursor 6 ));
                    new MenuItem("-");
                    new MenuItem("Equals", new System.EventHandler(fun _ _ -> pnl.insertText("equals( , )"); pnl.moveCursor 7 ));
                    new MenuItem("Approx", new System.EventHandler(fun _ _ -> pnl.insertText("approx( , )"); pnl.moveCursor 7 ));
                    new MenuItem("Major", new System.EventHandler(fun _ _ -> pnl.insertText("major( , )"); pnl.moveCursor 6 ));
                    new MenuItem("Minor", new System.EventHandler(fun _ _ -> pnl.insertText("minor( , )"); pnl.moveCursor 6 ));
                    new MenuItem("Greater or Equal", new System.EventHandler(fun _ _ -> pnl.insertText("geq( , )"); pnl.moveCursor 4 ));
                    new MenuItem("Less or Equal", new System.EventHandler(fun _ _ -> pnl.insertText("leq( , )"); pnl.moveCursor 4 ));
                    new MenuItem("-");
                    new MenuItem("Left arrow", new System.EventHandler(fun _ _ -> pnl.insertText("leftarrow( , )"); pnl.moveCursor 10 ));
                    new MenuItem("Right arrow", new System.EventHandler(fun _ _ -> pnl.insertText("rightarrow( , )"); pnl.moveCursor 11 ));
                    new MenuItem("-");
                    new MenuItem("Concat", new System.EventHandler(fun _ _ -> pnl.insertText("concat( , )"); pnl.moveCursor 7 ));
                    new MenuItem("-");
                    new MenuItem("Matrix 2x2", new System.EventHandler(fun _ _ -> pnl.insertText("matrix(2, 2, , , , )"); pnl.moveCursor 13 ));
                    new MenuItem("Matrix 4x4", new System.EventHandler(fun _ _ -> pnl.insertText("matrix(4, 4, , , , )"); pnl.moveCursor 13 ));
               |]
    
    

    let menuletters = [|
                     new MenuItem("Alpha", new System.EventHandler(fun _ _ -> pnl.insertText("\\alpha"); pnl.moveCursor 6 ));
                     new MenuItem("Beta", new System.EventHandler(fun _ _ -> pnl.insertText("\\beta"); pnl.moveCursor 5 ));
                     new MenuItem("Gamma", new System.EventHandler(fun _ _ -> pnl.insertText("\\gamma"); pnl.moveCursor 6 ));
                     new MenuItem("Delta", new System.EventHandler(fun _ _ -> pnl.insertText("\\delta"); pnl.moveCursor 6 ));
                     new MenuItem("Epsilon", new System.EventHandler(fun _ _ -> pnl.insertText("\\epsilon"); pnl.moveCursor 8 ));
                     new MenuItem("Zeta", new System.EventHandler(fun _ _ -> pnl.insertText("\\zeta"); pnl.moveCursor 5 ));
                     new MenuItem("Eta", new System.EventHandler(fun _ _ -> pnl.insertText("\\eta"); pnl.moveCursor 4 ));
                     new MenuItem("Theta", new System.EventHandler(fun _ _ -> pnl.insertText("\\theta"); pnl.moveCursor 6 ));
                     new MenuItem("Iota", new System.EventHandler(fun _ _ -> pnl.insertText("\\iota"); pnl.moveCursor 5 ));
                     new MenuItem("Kappa", new System.EventHandler(fun _ _ -> pnl.insertText("\\kappa"); pnl.moveCursor 6 ));
                     new MenuItem("Lampda", new System.EventHandler(fun _ _ -> pnl.insertText("\\lampda"); pnl.moveCursor 6 ));
                     new MenuItem("Mu", new System.EventHandler(fun _ _ -> pnl.insertText("\\mu"); pnl.moveCursor 3 ));
                     new MenuItem("Nu", new System.EventHandler(fun _ _ -> pnl.insertText("\\nu"); pnl.moveCursor 3 ));
                     new MenuItem("Xi", new System.EventHandler(fun _ _ -> pnl.insertText("\\xi"); pnl.moveCursor 6 ));
                     new MenuItem("Omicron", new System.EventHandler(fun _ _ -> pnl.insertText("\\omicron"); pnl.moveCursor 8 ));
                     new MenuItem("Pi", new System.EventHandler(fun _ _ -> pnl.insertText("\\pi"); pnl.moveCursor 3 ));
                     new MenuItem("Rho", new System.EventHandler(fun _ _ -> pnl.insertText("\\rho"); pnl.moveCursor 4 ));
                     new MenuItem("Sigma", new System.EventHandler(fun _ _ -> pnl.insertText("\\sigma"); pnl.moveCursor 6 ));
                     new MenuItem("Tau", new System.EventHandler(fun _ _ -> pnl.insertText("\\tau"); pnl.moveCursor 4 ));
                     new MenuItem("Upsilon", new System.EventHandler(fun _ _ -> pnl.insertText("\\upsilon"); pnl.moveCursor 8 ));
                     new MenuItem("Phi", new System.EventHandler(fun _ _ -> pnl.insertText("\\phi"); pnl.moveCursor 4 ));
                     new MenuItem("Chi", new System.EventHandler(fun _ _ -> pnl.insertText("\\chi"); pnl.moveCursor 6 ));
                     new MenuItem("Psi", new System.EventHandler(fun _ _ -> pnl.insertText("\\psi"); pnl.moveCursor 6 ));
                     new MenuItem("Omega", new System.EventHandler(fun _ _ -> pnl.insertText("\\omega"); pnl.moveCursor 6 ));
                     new MenuItem("-");
                     new MenuItem("Infinity", new System.EventHandler(fun _ _ -> pnl.insertText("\\infty"); pnl.moveCursor 6 ));
                |]
    mexpr.MenuItems.AddRange(menuexpr)
    mletter.MenuItems.AddRange(menuletters)
    f.Menu <- m
    let sb = new StatusBar()
    sb.Text <- "Ok"
    pnl.ExprUpdated.Add(fun (sender, str) -> sb.Text <- str)

    f.Controls.Add(sb)
    f.Text <- "Math Visualizer - salvo.dg@gmail.com"
    

    Application.Run(f)
